/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.class')
        .controller('ClassInfoCtrl', ClassInfoCtrl);

    function ClassInfoCtrl($scope,ClassInfoModel,AppUtils,LoginAccountAuth,$ionicSlideBoxDelegate,MemberModel) {

        $scope.next = function() {
            $ionicSlideBoxDelegate.next();
        };
        $scope.previous = function() {
            $ionicSlideBoxDelegate.previous();
        };
        $scope.slideChanged = function(index) {
            $scope.slideIndex = index;
        };

        $ionicSlideBoxDelegate.loop(true); //解决轮播至最后一个不轮播的问题



        var vm = this;
        vm.loginAccount = {};
        vm.classInfo = {};
        LoginAccountAuth.getAccount().then(function(data){
            vm.loginAccount = data;
            var memberId = "";
            if(vm.loginAccount.memberType == 2){
                memberId = vm.loginAccount.studentId;
            }else{
                memberId = vm.loginAccount.memberId;
            }
            ClassInfoModel.classInfo({memberId: memberId}).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    vm.classInfo = response.schoolClass;
                    vm.getStudentPage();
                }
            })
        })

        vm.items = [];
        vm.pageIndex = 1;
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";

        $scope.loadMoreEntrust = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.getStudentPage();
            }
        }

        vm.getStudentPage = function(){
            MemberModel.getStudentList({
                classId: vm.loginAccount.classId,
                pageInfo: {
                    pageSize: 10,
                    pageNumber: vm.pageIndex
                }
            }).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                    angular.forEach(items,function(data){
                        vm.items.push(data);
                    })
                }
            })
        }

    }

})();
